/*
 *  Copyright 2012 yura.
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */

package org.opu.db.obj.vis.ui.actions;

import java.awt.event.ActionEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.List;

import org.opu.db.obj.vis.domain.ObjectService;
import org.opu.db.obj.vis.domain.domains.AbstractAttributeEntry;
import org.opu.db.obj.vis.domain.domains.AbstractEntity;
import org.opu.db.obj.vis.domain.domains.ObjectEntry;
import org.opu.db.obj.vis.ui.client.MainView;
import org.opu.db.obj.vis.ui.client.widgets.ObjectEditPanel;
import org.opu.db.obj.vis.ui.util.Options;

/**
 *
 * @author yura
 */
public class Details extends AbstractAction implements PropertyChangeListener {

	private static final long serialVersionUID = -3630423340171932188L;

	protected ObjectService objectService;
    private boolean canBeAnable = false;
	
	public void setObjectService(ObjectService objectService) {
		this.objectService = objectService;
	}

    @Override
    public void action(ActionEvent e) throws Exception {
        List<? extends AbstractEntity> selected = mainView.getSelectedBeans();
        if (selected == null || selected.size() != 1){
            Options.error(getSafety(getName() + ".err1", "You can perform this"
                    + " action only if you select ONE object"));
            return;
        }

        AbstractEntity get = selected.get(0);

        if (get instanceof ObjectEntry){
            performEditing((ObjectEntry) get);
            saveEditedObject((ObjectEntry) get);
        } else if (get instanceof AbstractAttributeEntry){
            performEditing((AbstractAttributeEntry) get);
            saveEditedObject((AbstractAttributeEntry) get);
        }

        onActionFinish();
    }

    @Override
    public String getName() {
        return "Details";
    }

	@Override
	public void setMainView(MainView mainView) {
		super.setMainView(mainView);
		mainView.addChangeListener(this);
	}

	@SuppressWarnings("rawtypes")
	@Override
	public void propertyChange(PropertyChangeEvent evt) {
        Object newValue = evt.getNewValue();

        if (MainView.SELECTED_BEANS.equals(evt.getPropertyName())){
			List list = (List) newValue;
			setEnabled((list != null && list.size() == 1) && canBeAnable);
		} else if (MainView.CONTENT_TYPE.equals(evt.getPropertyName())){
            Class<?> clazz = (Class<?>) newValue;
			canBeAnable = clazz != null &&
                    (ObjectEntry.class.isAssignableFrom(clazz)
                    || AbstractAttributeEntry.class.isAssignableFrom(clazz));
		}
	}

    protected void performEditing(ObjectEntry object){
        ObjectEditPanel editPanel = new ObjectEditPanel(object);
        editPanel.setVisible(true);
    }

    protected void performEditing(AbstractAttributeEntry attribute){

    }

    private void saveEditedObject(ObjectEntry object) {

    }

    private void saveEditedObject(AbstractAttributeEntry attribute) {

    }

}
